package cn.stylefeng.guns.modular.file.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.file.entity.FileEntity;
import cn.stylefeng.guns.modular.file.mapper.FileEntityMapper;
import cn.stylefeng.guns.modular.file.model.params.FileEntityParam;
import cn.stylefeng.guns.modular.file.model.result.FileEntityResult;
import  cn.stylefeng.guns.modular.file.service.FileEntityService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件实体管理 服务实现类
 * </p>
 *
 * @author rebiekong
 * @since 2019-10-20
 */
@Service
public class FileEntityServiceImpl extends ServiceImpl<FileEntityMapper, FileEntity> implements FileEntityService {

    @Override
    public void add(FileEntityParam param){
        FileEntity entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FileEntityParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FileEntityParam param){
        FileEntity oldEntity = getOldEntity(param);
        FileEntity newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FileEntityResult findBySpec(FileEntityParam param){
        return null;
    }

    @Override
    public List<FileEntityResult> findListBySpec(FileEntityParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FileEntityParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FileEntityParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FileEntity getOldEntity(FileEntityParam param) {
        return this.getById(getKey(param));
    }

    private FileEntity getEntity(FileEntityParam param) {
        FileEntity entity = new FileEntity();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
